#include "menu_fsm.h"
#include "mission_1_fsm.h"
#include "mission_2_fsm.h"
#include "mission_3_fsm.h"
#include "mission_4_fsm.h"
#include "./OLED/OLED.h"

/********************全局变量定义********************/
lwq_t menu_fsm_que; /*菜单状态机的消息队列*/
state_machine_t menu_fsm; /*菜单状态机*/

/********************静态变量定义********************/
 

/********************静态函数定义********************/
static void page_1_handle(state_machine_t* me, param_event_t* e);
static void page_2_handle(state_machine_t* me, param_event_t* e);
static void page_3_handle(state_machine_t* me, param_event_t* e);
static void page_4_handle(state_machine_t* me, param_event_t* e);
static void page_mission_1_handle(state_machine_t* me, param_event_t* e);
static void page_mission_2_handle(state_machine_t* me, param_event_t* e);
static void page_mission_3_handle(state_machine_t* me, param_event_t* e);
static void page_mission_4_handle(state_machine_t* me, param_event_t* e);
static void page_1_load(void);
static void page_2_load(void);
static void page_3_load(void);
static void page_4_load(void);
static void page_mission_1_load(void);
static void page_mission_2_load(void);
static void page_mission_3_load(void);
static void page_mission_4_load(void);

/********************全局函数定义********************/

/**
 * @brief	菜单状态机初始化
 */
void menu_fsm_init(void)
{
	lwq_create(&menu_fsm_que, QUE_MENU_MSG_LEN, sizeof(param_event_t)); /*动态创建菜单消息队列*/
	menu_fsm.state = PAGE_1; /*设置初始状态*/
	page_1_load(); /*显示第一个页面*/
}

/**
 * @brief	菜单状态机调度函数
 * @param	me 状态机变量
 * @param	event 事件变量
 */
void menu_fsm_dispatch(state_machine_t* me, param_event_t* e)
{
	switch(me->state)
	{
		case PAGE_1:
			page_1_handle(me, e);
			break;
		
		case PAGE_2:
			page_2_handle(me, e);
			break;
		
		case PAGE_3:
			page_3_handle(me, e);
			break;
		
		case PAGE_4:
			page_4_handle(me, e);
			break;
		
		case MISSION_1:
			page_mission_1_handle(me, e);
			break;
		
		case MISSION_2:
			page_mission_2_handle(me, e);
			break;
		
		case MISSION_3:
			page_mission_3_handle(me, e);
			break;
		
		case MISSION_4:
			page_mission_4_handle(me, e);
			break;
	}
}

/********************静态函数定义********************/

/*页面1事件处理*/
static void page_1_handle(state_machine_t* me, param_event_t* e)
{
	switch(e->event)
	{
		case DOWN_SIG:
		{
			OLED_Clear();
			page_2_load();
			me->state = PAGE_2;
			break;
		}
		
		case CONFIRM_SIG:
		{
			param_event_t send_event;
			send_event.event = MISSION_1_ENTER;
			send_event.data = NULL;
			OLED_Clear();
			page_mission_1_load();
			lwq_write(&mission_1_fsm_que, &send_event); /*发送进入事件*/
			me->state = MISSION_1;
			break;
		}
	}
}

/*页面2事件处理*/
static void page_2_handle(state_machine_t* me, param_event_t* e)
{
	switch(e->event)
	{
		case DOWN_SIG:
			OLED_Clear();
			page_3_load();
			me->state = PAGE_3;
			break;
		
		case CONFIRM_SIG:
		{
			param_event_t send_event;
			send_event.event = MISSION_2_ENTER;
			send_event.data = NULL;
			OLED_Clear();
			page_mission_2_load();
			lwq_write(&mission_2_fsm_que, &send_event); /*发送进入事件*/
			me->state = MISSION_2;
			break;
		}
	}
}

/*页面3事件处理*/
static void page_3_handle(state_machine_t* me, param_event_t* e)
{
	switch(e->event)
	{
		case DOWN_SIG:
			OLED_Clear();
			page_4_load();
			me->state = PAGE_4;
			break;
		
		case CONFIRM_SIG:
		{
			param_event_t send_event;
			send_event.event = MISSION_3_ENTER;
			send_event.data = NULL;
			OLED_Clear();
			page_mission_3_load();
			lwq_write(&mission_3_fsm_que, &send_event); /*发送进入事件*/
			me->state = MISSION_3;
			break;
		}
	}
}

/*页面4事件处理*/
static void page_4_handle(state_machine_t* me, param_event_t* e)
{
	switch(e->event)
	{
		case DOWN_SIG:
			OLED_Clear();
			page_1_load();
			me->state = PAGE_1;
			break;
		
		case CONFIRM_SIG:
		{
			param_event_t send_event;
			send_event.event = MISSION_4_ENTER;
			send_event.data = NULL;
			OLED_Clear();
			page_mission_4_load();
			lwq_write(&mission_4_fsm_que, &send_event); /*发送进入事件*/
			me->state = MISSION_4;
			break;
		}
	}
}

/*任务1页面处理*/
static void page_mission_1_handle(state_machine_t* me, param_event_t* e)
{
	switch(e->event)
	{
		case RETURN_SIG:
		{
			param_event_t send_event;
			send_event.event = MISSION_1_EXIT; 
			send_event.data = NULL;
			OLED_Clear();
			page_1_load();
			lwq_write(&mission_1_fsm_que, &send_event); /*发送退出事件*/
			me->state = PAGE_1;
			break;
		}
	}
}

/*任务2页面处理*/
static void page_mission_2_handle(state_machine_t* me, param_event_t* e)
{
	switch(e->event)
	{
		case RETURN_SIG:
		{
			param_event_t send_event;
			send_event.event = MISSION_2_EXIT; 
			send_event.data = NULL;
			OLED_Clear();
			page_2_load();
			lwq_write(&mission_2_fsm_que, &send_event); /*发送退出事件*/
			me->state = PAGE_2;
			break;
		}
	}
}

/*任务3页面处理*/
static void page_mission_3_handle(state_machine_t* me, param_event_t* e)
{
	switch(e->event)
	{
		case RETURN_SIG:
		{
			param_event_t send_event;
			send_event.event = MISSION_3_EXIT; 
			send_event.data = NULL;
			OLED_Clear();
			page_3_load();
			lwq_write(&mission_3_fsm_que, &send_event); /*发送退出事件*/
			me->state = PAGE_3;
			break;
		}
	}
}

/*任务4页面处理*/
static void page_mission_4_handle(state_machine_t* me, param_event_t* e)
{
	switch(e->event)
	{
		case RETURN_SIG:
		{
			param_event_t send_event;
			send_event.event = MISSION_4_EXIT; 
			send_event.data = NULL;
			OLED_Clear();
			page_4_load();
			lwq_write(&mission_4_fsm_que, &send_event); /*发送退出事件*/
			me->state = PAGE_4;
			break;
		}			
	}
}

/*页面1载入函数*/
static void page_1_load(void)
{
	OLED_ShowString(0, 0, "mission_1 <-", OLED_6X8);
	OLED_ShowString(0, 8, "mission_2", OLED_6X8);
	OLED_ShowString(0, 16, "mission_3", OLED_6X8);
	OLED_ShowString(0, 24, "mission_4", OLED_6X8);
}

/*页面2载入函数*/
static void page_2_load(void)
{
	OLED_ShowString(0, 0, "mission_1", OLED_6X8);
	OLED_ShowString(0, 8, "mission_2 <-", OLED_6X8);
	OLED_ShowString(0, 16, "mission_3", OLED_6X8);
	OLED_ShowString(0, 24, "mission_4", OLED_6X8);
}

/*页面3载入函数*/
static void page_3_load(void)
{
	OLED_ShowString(0, 0, "mission_1", OLED_6X8);
	OLED_ShowString(0, 8, "mission_2", OLED_6X8);
	OLED_ShowString(0, 16, "mission_3 <-", OLED_6X8);
	OLED_ShowString(0, 24, "mission_4", OLED_6X8);
}

/*页面4载入函数*/
static void page_4_load(void)
{
	OLED_ShowString(0, 0, "mission_1", OLED_6X8);
	OLED_ShowString(0, 8, "mission_2", OLED_6X8);
	OLED_ShowString(0, 16, "mission_3", OLED_6X8);
	OLED_ShowString(0, 24, "mission_4 <-", OLED_6X8);
}

/*任务1页面载入函数*/
static void page_mission_1_load(void)
{
	OLED_ShowString(24, 0, "mission_1", OLED_8X16);
	OLED_ShowString(24, 16, "is running", OLED_8X16);
	OLED_ShowString(24, 48, "-> back <-", OLED_8X16);
}

/*任务2页面载入函数*/
static void page_mission_2_load(void)
{
	OLED_ShowString(24, 0, "mission_2", OLED_8X16);
	OLED_ShowString(24, 16, "is running", OLED_8X16);
	OLED_ShowString(24, 48, "-> back <-", OLED_8X16);
}

/*任务3页面载入函数*/
static void page_mission_3_load(void)
{
	OLED_ShowString(24, 0, "mission_3", OLED_8X16);
	OLED_ShowString(24, 16, "is running", OLED_8X16);
	OLED_ShowString(24, 48, "-> back <-", OLED_8X16);
}

/*任务4页面载入函数*/
static void page_mission_4_load(void)
{
	OLED_ShowString(24, 0, "mission_4", OLED_8X16);
	OLED_ShowString(24, 16, "is running", OLED_8X16);
	OLED_ShowString(24, 48, "-> back <-", OLED_8X16);
}
